﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.UI;
using MediaBrowser.Library;
using MediaBrowser.Library.Persistance;
using MediaBrowser.Library.Configuration;


namespace Chocolate
{
    public class MyConfig : ModelItem
    {
        private ConfigData data;

        public MyConfig()
        {
            isValid = Load();
        }

        private bool isValid;

        private void Save()
        {
            lock (this)
                this.data.Save();
        }

        private bool Load()
        {
            try
            {
                this.data = ConfigData.FromFile(System.IO.Path.Combine(ApplicationPaths.AppPluginPath, "Configurations\\ChocolateSettings.xml"));
                return true;
            }
            catch (Exception ex)
            {
                MediaCenterEnvironment ev = Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment;
                DialogResult r = ev.Dialog(ex.Message + "\nReset to default?", "Error in configuration file", DialogButtons.Yes | DialogButtons.No, 600, true);
                if (r == DialogResult.Yes)
                {
                    if (!Directory.Exists(System.IO.Path.Combine(ApplicationPaths.AppPluginPath, "Configurations"))) Directory.CreateDirectory(System.IO.Path.Combine(ApplicationPaths.AppPluginPath, "Configurations"));
                    this.data = new ConfigData(System.IO.Path.Combine(ApplicationPaths.AppPluginPath, "Configurations\\ChocolateSettings.xml"));
                    Save();
                    return true;
                }
                else
                    return false;
            }
        }

        public bool ShowInfoIcons
        {
            get { return data.showInfoIcons; }
            set
            {
                if (data.showInfoIcons != value)
                { data.showInfoIcons = value; FirePropertyChanged("ShowInfoIcons"); Save(); }
            }
        }
        public bool ShowInfoIconsBackground
        {
            get { return data.showInfoIconsBackground; }
            set
            {
                if (data.showInfoIconsBackground != value)
                { data.showInfoIconsBackground = value; FirePropertyChanged("ShowInfoIconsBackground"); Save(); }
            }
        }
        public bool SmallEhs
        {
            get { return data.smallEhs; }
            set
            {
                if (data.smallEhs != value)
                { data.smallEhs = value; FirePropertyChanged("SmallEhs"); Save(); }
            }
        }

        public bool UseSeasonPoster
        {
            get { return data.useSeasonPoster; }
            set
            {
                if (data.useSeasonPoster != value)
                { data.useSeasonPoster = value; FirePropertyChanged("UseSeasonPoster"); Save(); }
            }
        }

        public bool AlwaysShowPoster
        {
            get { return data.alwaysShowPoster; }
            set
            {
                if (data.alwaysShowPoster != value)
                { data.alwaysShowPoster = value; FirePropertyChanged("AlwaysShowPoster"); Save(); }
            }
        }

        public bool StripviewExpanded
        {
            get { return data.stripviewExpanded; }
            set
            {
                if (data.stripviewExpanded != value)
                { data.stripviewExpanded = value; FirePropertyChanged("StripviewExpanded"); Save(); }
            }
        }

        public bool ShowWeather
        {
            get { return data.showWeather; }
            set
            {
                if (data.showWeather != value)
                { data.showWeather = value; FirePropertyChanged("ShowWeather"); Save(); }
            }
        }

        public string MaintainBackdropAR
        {
            get 
            { 
                if (data.maintainBackdropAR != "Stretch" && data.maintainBackdropAR != "Fit" && data.maintainBackdropAR != "Fill")
                {
                    data.maintainBackdropAR = "Stretch"; FirePropertyChanged("MaintainBackdropAR"); Save();
                }
                return data.maintainBackdropAR; 
            }
            set
            {
                if (data.maintainBackdropAR != value)
                { data.maintainBackdropAR = value; FirePropertyChanged("MaintainBackdropAR"); Save(); }
            }
        }

        public bool EnableVideoBackdrop
        {
            get { return data.enableVideoBackdrop; }
            set
            {
                if (data.enableVideoBackdrop != value)
                { data.enableVideoBackdrop = value; FirePropertyChanged("EnableVideoBackdrop"); Save(); }
            }
        }

        public bool EnableEHSBackdrop
        {
            get { return data.enableEHSBackdrop; }
            set
            {
                if (data.enableEHSBackdrop != value)
                { data.enableEHSBackdrop = value; FirePropertyChanged("EnableEHSBackdrop"); Save(); }
            }
        }

        public bool EnableStripBackdrop
        {
            get { return data.enableStripBackdrop; }
            set
            {
                if (data.enableStripBackdrop != value)
                { data.enableStripBackdrop = value; FirePropertyChanged("EnableStripBackdrop"); Save(); }
            }
        }

        public bool EnableFolderBackdrop
        {
            get { return data.enableFolderBackdrop; }
            set
            {
                if (data.enableFolderBackdrop != value)
                { data.enableFolderBackdrop = value; FirePropertyChanged("EnableFolderBackdrop"); Save(); }
            }
        }

        public bool Enable24hr
        {
            get { return data.enable24hr; }
            set
            {
                if (data.enable24hr != value)
                { data.enable24hr = value; FirePropertyChanged("Enable24hr"); Save(); }
            }
        }

        public bool ShowLogos
        {
            get { return data.showLogos; }
            set
            {
                if (data.showLogos != value)
                { data.showLogos = value; FirePropertyChanged("ShowLogos"); Save(); }
            }
        }

    }

    [Serializable]
    public class ConfigData
    {
        public ConfigData() { }

        public ConfigData(string file)
        {
            this.file = file;
            this.ChocolateSettings = XmlSettings<ConfigData>.Bind(this, file);
        }

        public bool showInfoIcons = true;
        public bool showInfoIconsBackground = true;
        public bool smallEhs = false;
        public bool useSeasonPoster = true;
        public bool alwaysShowPoster = false;
        public bool stripviewExpanded = false;
        public bool showWeather = false;
        public string maintainBackdropAR = "Stretch";
        public bool enableVideoBackdrop = true;
        public bool enableEHSBackdrop = false;
        public bool enableStripBackdrop = false;
        public bool enableFolderBackdrop = false;
        public bool enable24hr = false;
        public bool showLogos = false;

        [SkipField]
        string file;

        [SkipField]
        XmlSettings<ConfigData> ChocolateSettings;


        public static ConfigData FromFile(string file)
        {
            return new ConfigData(file);
        }

        public void Save()
        {
            this.ChocolateSettings.Write();
        }

    }
}
